package de.fhd.pms.dao;

import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.fhd.pms.model.Auto;
import de.fhd.pms.model.Station;

/**
 * The Data access class for {@link Station} objects. DAO encapsulates all
 * accesses to the data source, including construction and dismantling of the
 * connection. All Interaction with the database regarding the entity bean
 * {@link Station} should be handled by this class! Its tasks:
 * <ul>
 * <li>set new objects of the class</li>
 * <li>modify or delete an object of the class</li>
 * <li>deliver all objects of the class</li>
 * <li>further functions in context to the class objects</li>
 * </ul>
 * 
 * @author Josef Fischer
 * @author Alexander Neetzel
 * @author Philipp Six
 */
public class StationDao extends HibernateDaoSupport {

	/**
	 * Returns a single {@link Station} object by its primary DB key
	 * 
	 * @param id
	 *            the primary key of {@link Station}
	 * @return a single {@link Station}
	 */
	public Station findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Station) template.get(Station.class, id);
	}

	/**
	 * Saves {@link Station} object specified by the parameter in the DB
	 * 
	 * @param station
	 *            is the object that'll be saved in the DB
	 * @return a {@link Station} object specified by the parameter
	 */
	public Station save(Station station) {
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(station);
		return station;
	}

	/**
	 * Creates a google map with a marker for every {@link Station} in the DB
	 * <ul>
	 * <li>It selects the highest {@link Station} ID from the DB for the
	 * loop-counter</li>
	 * <li>It checks if the query size is zero to avoid a empty array.</li>
	 * </ul>
	 * 
	 * @return a String with the link or null if there are no stations
	 */

	@SuppressWarnings("rawtypes")
	public String getGoogle() {
		String googleLink = "http://maps.google.com/maps/api/staticmap?center=48.139058,11.580276&zoom=11&size=600x600";
		String parameter = "";
		String temp = "";
		HibernateTemplate template = getHibernateTemplate();

		List query = template
				.find("select id FROM Station order by id desc limit 1 ");

		if (query.size() != 0) {
			Object[] tmp = query.toArray();
			int j = (Integer) tmp[0] + 1;
			for (int i = 1; i < j; i++) {

				if (this.findById(i) == null) {

				} else {
					temp = this.findById(i).getGoogle();
					parameter = parameter + "&markers=color:green|label:S|"
							+ temp;
				}
			}
			googleLink = googleLink + parameter + "&sensor=false";
			return googleLink;

		} else
			return null;
	}

	/**
	 * Creates a google map image with a marker for the specified {@link Auto}
	 * 
	 * @param id
	 * @return a String with the link
	 */

	public String getGoogleCar(int id) {
		HibernateTemplate template = getHibernateTemplate();
		// TODO testen
		Auto auto = (Auto) template.get(Auto.class, id);
		String parameter = auto.getStation().getGoogle();
		String googleLink = "http://maps.google.com/maps/api/staticmap?center=";
		googleLink = googleLink + parameter + "&zoom=15&size=600x600"
				+ "&markers=color:green|label:S|" + parameter + "&sensor=false";
		return googleLink;
	}

	/**
	 * Deletes the specified {@link Station} object from the DB
	 * 
	 * @param station
	 *            the object to be deleted
	 * @throws DeleteException
	 *             if a {@link Auto} is parked at the specified {@link Station}
	 */
	@SuppressWarnings("rawtypes")
	public void delete(Station station) throws DeleteException {
		HibernateTemplate template = getHibernateTemplate();
		List fahrtenVonAuto = template.find("FROM Auto a where a.station like "
				+ station.getId());
		if (fahrtenVonAuto.size() == 0) {
			template.delete(station);

		} else {

			throw new DeleteException(
					"Die Station darf nicht gelöscht werden, da darauf Autos geparkt sind.");
		}
	}

	/**
	 * Returns all {@link Station} objects from the DB
	 * 
	 * @return a list of {@link Station} objects
	 */
	@SuppressWarnings("unchecked")
	public List<Station> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		@SuppressWarnings("rawtypes")
		List results = template.loadAll(Station.class);
		return results;
	}

}
